home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / db / esm-3.1 / esm-3 / usr / local / sm / src / client / btree / OVPAGE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-05  |  5.0 KB  |  215 lines

  1. /*
  2.  * $RCSfile: OVPAGE.C,v $
  3.  * $Revision: 1.1.1.1 $
  4.  * $Date: 1996/05/04 21:55:16 $
  5.  */
  6. /**********************************************************************
  7. * EXODUS Database Toolkit Software
  8. * Copyright (c) 1991 Computer Sciences Department, University of
  9. *                    Wisconsin -- Madison
  10. * All Rights Reserved.
  11. *
  12. * Permission to use, copy, modify and distribute this software and its
  13. * documentation is hereby granted, provided that both the copyright
  14. * notice and this permission notice appear in all copies of the
  15. * software, derivative works or modified versions, and any portions
  16. * thereof, and that both notices appear in supporting documentation.
  17. *
  18. * THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY OF WISCONSIN --
  19. * MADISON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.  
  20. * THE DEPARTMENT DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES
  21. * WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  22. *
  23. * The EXODUS Project Group requests users of this software to return 
  24. * any improvements or extensions that they make to:
  25. *
  26. *   EXODUS Project Group 
  27. *     c/o David J. DeWitt and Michael J. Carey
  28. *   Computer Sciences Department
  29. *   University of Wisconsin -- Madison
  30. *   Madison, WI 53706
  31. *
  32. *     or exodus@cs.wisc.edu
  33. *
  34. * In addition, the EXODUS Project Group requests that users grant the 
  35. * Computer Sciences Department rights to redistribute these changes.
  36. **********************************************************************/
  37. #include "BTREEPAGE.h"
  38. #include "OVPAGE.h"
  39. #include "sm_macro.h"
  40.  
  41.  
  42. void OVPAGE::Init(PID& pid, int es)    
  43. {
  44.     ovCtrl.elCnt = 0;
  45.     ovCtrl.elSize = es;
  46.     ovCtrl.maxEl = OV_DATASIZE / es;
  47.     ovCtrl.selfID = pid;
  48.     ovCtrl.next = ovCtrl.prev = NULLPID;
  49. }
  50.  
  51. //
  52. //    Class        : OVPAGE
  53. //    Method        : void Linkup(OVPAGE* rightPage)
  54. //    Description    : Link this page to rightPage. Update next and prev pointers.
  55. //
  56. void OVPAGE::Linkup(OVPAGE* rightPage)
  57. {
  58.     ovCtrl.next = NULLPID;
  59.     if (rightPage)  {
  60.     rightPage->ovCtrl.prev = ovCtrl.selfID.page;
  61.         ovCtrl.next = rightPage->ovCtrl.selfID.page;
  62.     }
  63. }
  64.  
  65.  
  66.  
  67. //
  68. //    Class        : OVPAGE
  69. //    Method        : void Shift(int start, OVPAGE* destPage)
  70. //    Description    : Shifts all tuples from this page starting 
  71. //              from slot 'start' to end of destPage.
  72. //
  73. void OVPAGE::Shift(int start, OVPAGE* destPage)
  74. {
  75.     int cnt = ElCnt() - start;        // # of oids to move
  76.     
  77.     //
  78.     //    Assert sane parameters 
  79.     //
  80.     ASSERT3(start >= 0 && start < ElCnt());
  81.     ASSERT3(destPage->ElCnt() + cnt < MaxEl());
  82.     ASSERT3(destPage->ElSize() == ElSize());
  83.     
  84.     //
  85.     //    Move it
  86.     //
  87.     void* op1 = destPage->Get(destPage->ovCtrl.elCnt);
  88.     void* op2 = Get(start);
  89.     BCOPY(op2, op1, ElSize() * cnt);
  90.     
  91.     //
  92.     //    Update control info
  93.     //    
  94.     destPage->ovCtrl.elCnt += cnt;
  95.     ovCtrl.elCnt = start;
  96.     
  97.     ASSERT3(CheckPage());
  98.     ASSERT3(destPage->CheckPage());
  99. }
  100.  
  101.  
  102. void OVPAGE::FillSort(int cnt, void* array)
  103. {
  104.     ASSERT3(ovCtrl.elCnt == 0);
  105.     
  106.     BCOPY(array, Get(0), cnt * ElSize());
  107.     ovCtrl.elCnt = cnt;
  108.     
  109.     Sort();
  110.     
  111.     ASSERT3(CheckPage());
  112. }
  113.  
  114.  
  115. //
  116. //    Class        : OVPAGE
  117. //    Method        : void Insert(int startSlot, int cnt, void* array)
  118. //    Description    : Insert cnt entries of 'array' into this page starting
  119. //                  at startSlot.
  120. //
  121. void OVPAGE::Insert(int startSlot, int cnt, const void* array)
  122. {
  123.     ASSERT3(startSlot >= 0 && startSlot <= ElCnt());
  124.     
  125.     //
  126.     //    Make space in oidArray
  127.     //
  128.     if (ElCnt() > startSlot)  {
  129.     BCOPY(Get(startSlot), Get(startSlot + cnt),
  130.           (ElCnt() - startSlot) * ElSize());
  131.     }
  132.     
  133.     //
  134.     //    Copy into the space in oidArray
  135.     //
  136.     BCOPY(array, Get(startSlot), cnt * ElSize());
  137.     
  138.     //
  139.     //    Update control info
  140.     //
  141.     ovCtrl.elCnt += cnt;
  142.     
  143.     ASSERT3(CheckPage());
  144. }
  145.  
  146.  
  147. //
  148. //    Class        : OVPAGE
  149. //    Method        : void Remove(int startSlot, int cnt)
  150. //    Description    : Remove cnt entries starting from startSlot
  151. //
  152. void OVPAGE::Remove(int startSlot, int cnt)
  153. {
  154.     ASSERT3(cnt > 0);
  155.     ASSERT3(ElCnt() >= startSlot + cnt);
  156.     
  157.     //
  158.     //    Close the gap in oidArray
  159.     //
  160.     if (ElCnt() > startSlot + cnt)  {
  161.     BCOPY(Get(startSlot + cnt), Get(startSlot), 
  162.           (ElCnt()-(startSlot+cnt)) * ElSize());
  163.     }
  164.     
  165.     //
  166.     //    Update control info
  167.     //
  168.     ovCtrl.elCnt -= cnt;
  169.     ASSERT3(CheckPage());
  170. }
  171.  
  172.  
  173.  
  174. //
  175. //    Class        : OVPAGE
  176. //    Method        : void Sort()
  177. //    Description    : Sort the oidArray. A hack is needed in order to
  178. //              use the qsort() library routine. The static variable
  179. //              __OVSortElSize is used for this purpose.
  180. //
  181.  
  182. static int __OVSortElSize = 0;
  183. static int cmp(const void* o1, const void* o2);
  184.  
  185.  
  186. void OVPAGE::Sort()
  187. {
  188.     __OVSortElSize = ElSize();                    // used by cmp()
  189.     qsort(Get(0), ElCnt(), ElSize(), cmp);
  190. }
  191.  
  192.  
  193. static int cmp(const void* o1, const void* o2)
  194. {
  195.     ASSERT3(__OVSortElSize > 0 && __OVSortElSize <= SM_MAXELEMLEN);
  196.     return BT_ElemDiff(__OVSortElSize, o1, o2);
  197. }
  198.  
  199.  
  200.  
  201. //
  202. //    Class        : OVPAGE
  203. //    Method        : CheckPage()
  204. //    Description    : Checks the content of the page
  205. //
  206. int OVPAGE::CheckPage()
  207. {
  208.     ASSERT1(ElCnt() >= 0);
  209.     for (int i = 0; i < ElCnt() - 1; i++)  {
  210.     ASSERT1(BT_ElemDiff(ElSize(), Get(i), Get(i+1)) < 0);
  211.     }
  212.     
  213.     return TRUE;
  214. }
  215.